package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.entity.Orders;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.OrdersService;

import javax.xml.ws.soap.Addressing;
import java.util.Date;

/**
 * @Author milian
 * @Date 2021/9/28 0028 10:18
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @GetMapping("/page")
    public R<Page> getOrderDetailPage(@RequestParam("page") long pageNum, long pageSize, String number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date endTime){
        Page<Orders> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(number),Orders::getNumber,number).between(beginTime==null?false:true,Orders::getOrderTime,beginTime,endTime);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(page, lqw);
        return R.success(page);
    }
    @GetMapping("/userPage")
    public R<Page> getUserPage(@RequestParam("page") long pageNum, long pageSize){
        Page<Orders> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(page, lqw);
        return R.success(page);
    }
    @PostMapping("/submit")
    public R<String> submitOrder(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        ordersService.submit(orders);
        return R.success("提交成功");
    }
    @PutMapping
    public R<String> editOrderDetail(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");
    }
    @PostMapping("/again")
    public R orderAgain(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        ordersService.again(orders);
        return R.success("success");
    }
}
